import { UserConfigExport, ConfigEnv, loadEnv } from "vite";
import viteCompression from 'vite-plugin-compression';
// 开启代码压缩
import vue from "@vitejs/plugin-vue";
import path from "path";
import { viteMockServe } from "vite-plugin-mock";
import viteSvgIcons from "vite-plugin-svg-icons";

const setAlias = (alias: [string, string][]) =>
    alias.map((v) => {
        return { find: v[0], replacement: path.resolve(__dirname, v[1]) };
    });
const proxy = (list: [string, string][]) => {
    const obj: IObject<any> = {};
    list.forEach((v) => {
        obj[v[0]] = {
            target: v[1],
            changeOrigin: true,
            rewrite: (path: any) => path.replace(new RegExp(`^${v[0]}`), ""),
            ...(/^https:\/\//.test(v[1]) ? { secure: false } : {}),
        };
    });
    return obj;
};

export default ({ command, mode }: ConfigEnv): UserConfigExport => {
    const root = process.cwd();
    const env = loadEnv(mode, root) as unknown as ImportMetaEnv;
    // 控制产品文档是否打包 mock
    const prodMock = true;
    return {
        base: "./",
        resolve: {
            alias: setAlias([
                ["/@", "src"],
                ["/mock", "mock"],
                ["/server", "server"],
            ]),
        },
        server: {
            proxy: env.VITE_PROXY ? proxy(JSON.parse(env.VITE_PROXY)) : {},
            port: env.VITE_PORT,
        },
        build: {
            // sourcemap: true,

            outDir: 'vue3-admin', //指定输出路径
            assetsDir: 'assets', // 指定生成静态资源的存放路径
            minify: 'terser', // 混淆器，terser构建后文件体积更小

            manifest: true,
            rollupOptions: {
                output: {
                    manualChunks: {
                        "element-plus": ["element-plus"],
                        echarts: ["echarts"],
                        pinyin: ["pinyin"],
                    },
                },
            },
            chunkSizeWarningLimit: 600,
        },
        plugins: [
            viteCompression(),
            vue(),
            viteMockServe({
                mockPath: "mock",
                localEnabled: command === "serve",
                prodEnabled: command !== "serve" && prodMock,
                //  这样可以控制关闭mock的时候不让mock打包到最终代码内
                injectCode: `
                import { setupProdMockServer } from '/mock/mockProdServer';
                setupProdMockServer();
                `,
            }),
            viteSvgIcons({
                // 指定需要缓存的图标文件夹
                iconDirs: [path.resolve(process.cwd(), "src/icons")],
                // 指定symbolId格式
                symbolId: "icon-[dir]-[name]",
            }),
        ],
        css: {
            postcss: {
                plugins: [require("autoprefixer"), require("tailwindcss"), require("postcss-nested"), require("postcss-simple-vars"), require("postcss-import")],
            },
        },
    };
};
